
import controllers.Application;
import models.Entities;
import models.IdeaDeveloper;
import models.Organization;
import models.OrganizationLead;
import models.Organizer;
import org.junit.Test;
import play.test.Fixtures;
import play.test.UnitTest;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Ousama this class for testing my methods in all 
 */
public class Ousama extends UnitTest {

    /**
     * @author Ousama
     * this test to test the cases of adding an entitiy in the Organization
     */
    @Test
    public void addingEntitiyInOrganization() {
        Fixtures.deleteAll();
        IdeaDeveloper dev = new IdeaDeveloper();
        dev.save();

        IdeaDeveloper dev2 = new IdeaDeveloper();
        dev2.save();

        OrganizationLead leader = OrganizationLead.create(dev);
        Organization org = Organization.create(leader, "Org1");
        assertEquals("Done Adding", org.addEntitiy("Ent1", dev));
        assertEquals("You Are Not Allowed", org.addEntitiy("Ent2", dev2));
        assertEquals("The Name Is Not Avaliable", org.addEntitiy("Ent1", dev));
        assertEquals(1, org.liesIn.size());
    }

    /**
     * @author Ousama
     * this test to test the cases of adding a sub entity in an entity
     */
    @Test
    public void addingSubEntityInEntity() {
        Fixtures.deleteAll();
        Entities.deleteAll();
        IdeaDeveloper dev = new IdeaDeveloper();
        dev.save();

        IdeaDeveloper dev2 = new IdeaDeveloper();
        dev2.save();

        OrganizationLead leader = OrganizationLead.create(dev);
        Organization org = Organization.create(leader, "Org1");
        assertEquals("Done Adding", org.addEntitiy("Ent1", dev));
        Entities addedEntity = Entities.find("name = ? and liesIn = ?", "Ent1", org).first();

        Organizer organizer = Organizer.find("ideaDeveloper = ?",dev).first();

        //assertEquals("You Are Not Allowed", addedEntity.addEntity("Ent2", dev2));

        

        assertEquals("You Are Not Allowed", addedEntity.addEntity("Ent2", dev2));
        assertEquals("The Name Is Not Avaliable", addedEntity.addEntity("Ent1", dev));
        assertEquals(1, addedEntity.managers.size());
        assertTrue(addedEntity.managers.contains(organizer));
        assertEquals(organizer, organizer.find("ideaDeveloper = ?", dev).first());

    }

    /**
     * @author OUSAMA
     * this test for testing the deletion of the entity and the sub entity
     */
    @Test
    public void deleteEntity() {
        Fixtures.deleteAll();
        IdeaDeveloper dev = new IdeaDeveloper();
        dev.save();
        OrganizationLead leader = OrganizationLead.create(dev);
        Organization org = Organization.create(leader, "Org1");
        assertEquals("Done Adding", org.addEntitiy("Ent1", dev));
        Entities addedEntity = Entities.find("name = ? and liesIn = ?", "Ent1", org).first();
        addedEntity.addEntity("Ent2", dev);
        addedEntity.delete2();
        assertEquals(0, Entities.count());
    }

    /**
     * @author Ousama
     * this test to test the application controller that gets the oganization managed by a developer
     */
    @Test
    public void checkManagedOrganization() {
        IdeaDeveloper dev = new IdeaDeveloper("ousama", "ousama", "ousama", "ousama", "1-1-1990", "ousama");
        dev.save();
        OrganizationLead lead = OrganizationLead.create(dev);
        lead.save();
        Organization org = Organization.create(lead, "org1");
        assertEquals(1, org.managers.size());
        assertEquals(1, lead.manages.size());
        assertEquals(1, OrganizationLead.count("ideaDeveloper = ?", dev));
        IdeaDeveloper dev2 = IdeaDeveloper.findById(dev.id);
        assertTrue(org.managers.contains(OrganizationLead.find("ideaDeveloper = ?", dev2).first()));


    }
}
